package VectorApproach;

import java.util.ArrayList;

public class LinearRegression { 

	double beta0 = 0, beta1 = 0;
    public ArrayList<Double> x = new ArrayList<Double>();
    public ArrayList<Double> y = new ArrayList<Double>();
	
	public void ACT001_insertData(double x, double y) {
		System.out.println(x + " " + y);
		this.x.add(x);
		this.y.add(y);
	}
	
	public void ACT002_regression() {
        int n = 0;
        // first pass: read in data, compute xbar and ybar
        double sumx = 0.0, sumy = 0.0, sumx2 = 0.0;
        while(n < x.size()) {
            sumx  += x.get(n);
            sumx2 += x.get(n) * x.get(n);
            sumy  += y.get(n);
            n++;
        }
        double xbar = sumx / n;
        double ybar = sumy / n;

        // second pass: compute summary statistics
        double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
        for (int i = 0; i < n; i++) {
            xxbar += (x.get(i) - xbar) * (x.get(i) - xbar);
            yybar += (y.get(i) - ybar) * (y.get(i) - ybar);
            xybar += (x.get(i) - xbar) * (y.get(i) - ybar);
        }
        beta1 = xybar / xxbar;
        beta0 = ybar - beta1 * xbar;

        // print results
        System.out.println("y   = " + beta1 + " * x + " + beta0);
	}
	
	public double ACT003_func(double x) {
		return this.beta1 * x + this.beta0;
	}
	
	public double ACT004_revertFunc(double y) {
		return ((y - this.beta0) / this.beta1);
	}
	
	public double[] GET001_coefficience() {
		double[] result = {this.beta1, this.beta0};
		return result;
	}
	
    public static void main(String[] args) { 
        
    }
}